<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
      <el-form-item label="群组信息">
        <el-link type="success" disabled>{{this.queryParams.title}}</el-link>
        <el-link type="primary" disabled>{{form.chatId}}</el-link>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.bot_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键词(分割符,)" prop="keyWord">
        <el-input v-model="form.keyWord" type="textarea" placeholder="多个关键词用英文逗号隔开，如:关键词1,关键词2" />
      </el-form-item>
      <el-form-item prop="config">
        <line-button ref="lineButton" label="回复内容" placeholder="请输入回复内容" :data="form.config"></line-button>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </div>
</template>

<script>
import {  getReply,  updateReply } from "@/api/bot/reply";
import { simpleListGroup } from "@/api/bot/group";
import lineButton from '@/components/bot/inline_button.vue'
export default {
  name: "ReplyEdit",
  components: {lineButton},
  dicts: ['bot_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      queryParams:{
        chatId:null,
        title:null,
      },
      // 表单校验
      rules: {
        chatId: [
          { required: true, message: "群组不能为空", trigger: "blur" }
        ],
        keyWord: [
          { required: true, message: "关键词不能为空", trigger: "blur" }
        ],
        config: [
          { required: true, message: "回复内容不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.queryParams = this.$route.query
    this.queryParams.title = this.$route.query.params.chatName
    this.handleUpdate()
  },
  methods: {
    //返回上个页面
    back(){
      this.$tab.closeOpenPage({ path: "/tg/reply" });
    },
    // 表单重置
    reset() {
      this.form = {
        id: this.queryParams.id,
        chatId: this.queryParams.chatId,
        keyWord: null,
        config: {
          chatId: this.queryParams.chatId,
          content:"",
          buttonRows: [
            []
          ],
        },
        remark: null,
        orderNum: null,
        status: '1',
        delFlag: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      if(this.form.id){
        getReply(this.form.id).then(response => {
          if(response.data){
            response.data.config = JSON.parse(response.data.config)
            //防止群变更类型时候时候配置里面的ID不变
            response.data.config.chatId = response.data.chatId
            this.form = response.data;
          }
        });
      }
    },
    /** 提交按钮 */
    submitForm() {
      let validateEmpty = this.$refs.lineButton.validateEmpty();
      if(!validateEmpty.validate){
        this.$modal.msgWarning(validateEmpty.msg);
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateReply(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.back()
          });
        }
      });
    },
  }
};
</script>
